﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurnManager : MonoBehaviour
{
    public static Dictionary<string, List<BaseEntity>> units = new Dictionary<string, List<BaseEntity>>();
    public static Queue<string> belongTurnKey = new Queue<string>();
    public static Queue<BaseEntity> belongTurnTeam = new Queue<BaseEntity>();
    
    public delegate void SingleStringListenerDelegate(string val);
    public delegate void SingleNumListenerDelegate(int val);

    public static SingleStringListenerDelegate turnStringListener;
    public static SingleNumListenerDelegate turnNumListener;
    
    [NonSerialized]
    public static int currentTurnCount = 1;
    
    public static void InitTeamTurnQueue()
    {
        if (belongTurnKey.Count <= 0)
        {
            return;
        }
        
        List<BaseEntity> teamList = units[belongTurnKey.Peek()];

        foreach (BaseEntity unit in teamList)
        {
            try
            {
                if (unit && !unit.isDead)
                {
                    belongTurnTeam.Enqueue(unit);                
                }
            }
            catch (Exception e)
            {
                Debug.Log(e);
            }
        }

        StartTurn();
    }
    
    
    public static void StartTurn()
    {        
        TileManager.ResetAllTileScore();
        if (belongTurnTeam.Count <= 0) return;
        belongTurnTeam.Peek().StartTurn();
        
        FlushTurnText();
        currentTurnCount += 1;
        //TileManager.DisplayAllTileOverlay(Color.clear);

    }

    public static void EndTurn()
    {
        if (belongTurnTeam.Count > 0)
        {
            BaseEntity unit = belongTurnTeam.Dequeue();
            unit.EndTurn();
        }

        if (belongTurnTeam.Count > 0)
        {
            StartTurn();
        }
        else
        {
            string teamName = belongTurnKey.Dequeue();
            
            // TODO 这里负责切换显示的队伍回合
            
            belongTurnKey.Enqueue(teamName);
            InitTeamTurnQueue();
        }
        TileManager.HideAllTileOverlay();
    }

    public static void AddUnit(BaseEntity unit)
    {
        List<BaseEntity> list;

        if (!units.ContainsKey(unit.tag))
        {
            list = new List<BaseEntity>();
            units[unit.tag] = list;

            if (!belongTurnKey.Contains(unit.tag))
            {
                belongTurnKey.Enqueue(unit.tag);
            }
        }
        else
        {
            list = units[unit.tag];
        }

        list.Add(unit);
    }

    private void Start()
    {
        units.Clear();
        belongTurnKey.Clear();
        belongTurnTeam.Clear();
    }

    private void Update()
    {
        if (belongTurnTeam.Count == 0)
        {
            InitTeamTurnQueue();
        }
    }

    public static void FlushTurnText()
    {
        turnStringListener(belongTurnTeam.Peek().tag);
        turnNumListener(currentTurnCount);
    }

    public void EndPlayerTurn()
    {
        if (belongTurnTeam.Peek().CompareTag(StaticDataManager.PLAYER_TAG))
        {
            belongTurnTeam.Peek().TurnEnd();
            EndTurn();
        }
    }

//    private void OnGUI()
//    {
//        try
//        {
//            var style = new GUIStyle {normal = {textColor = Color.white}, fontSize = 40};
//            GUILayout.Label($"当前的回合数 : {currentTurnCount}, 当前的回合属于 {belongTurnTeam?.Peek()?.GetType()}", style);
//        }
//        catch (Exception e)
//        {
//
//        }
//    }
}
